'''
shape-decode job.

Licensed under the GPL.

@author: screwdriver
'''

'''
Version history:
----------------
2009-03-29 screwdirver Created.
'''

import os;

from commandlinetool import ExcecuteCommand;
from commandlinetool import CommandLine;
from commandlinetool import Option;

from shapeconfig import ShapeFile;
from shapeconfig import Shape;
from shapeconfig import Line;
from shapeconfig import Point;

class ShapeDecoder(ExcecuteCommand):
	'''
	Shape decoding job.
	'''
	
	__SHAPE_DECODE_TOOL = "shape-decode";
	
	__MAX_SEGMENT_OPTION = "max-segment";
	__LINE_WIDTH_OPTION = "line-width";
	__POINT_SIZE_OPTION = "point-width";
	
	def __buildCommand(self, terragearRoot, shapefilesDir, projectDirectory, name, shapeType, material):
		
		tool = terragearRoot +  "/" + ShapeDecoder.__SHAPE_DECODE_TOOL;
		command = CommandLine(tool);
		
		shapeFile = shapefilesDir + "/" + name;
		command.arguments.append(shapeFile);
		
		outputDirectory = projectDirectory + "/" + material;
		command.arguments.append(outputDirectory);
		
		command.arguments.append(material);
		
		if (isinstance(shapeType, Shape)):
			maxSegment = shapeType.maxSegmentLength;
			maxSegmentOption = Option(ShapeDecoder.__MAX_SEGMENT_OPTION, maxSegment);
			command.options.append(maxSegmentOption);
			
			if (isinstance(shapeType, Line)):
				width = shapeType.width;
				lineWidthOption = Option(ShapeDecoder.__LINE_WIDTH_OPTION, width);
				command.options.append(lineWidthOption);					
		elif (isinstance(shapeType, Point)):
			size = shapeType.size;
			pointSizeOption = Option(ShapeDecoder.__POINT_SIZE_OPTION, size);
			command.options.append(pointSizeOption);
			
		return command;


	def __init__(self, shapeFile, terragearRoot, shapeFilesDir, projectDirectory):
		name = shapeFile.name;
		shapeType = shapeFile.shapeType;
		material = shapeFile.material;
		
		command = self.__buildCommand(terragearRoot, shapeFilesDir, projectDirectory, name, shapeType, material);
		
		commandline = str(command);
		
		ExcecuteCommand.__init__(self, commandline, "./logs/shape-decode." + name);
		
		